#-- vim:sw=2:et
#++
#
# :title: BorGate (bordom.net to #bor chat gateway)
#
# Author:: Jason Hines <jason@greenhell.com>
# Copyright:: (C) 2009 BoR/PLiK Heavy Development
# License:: N/A
#
# Docs: http://bordom.net/brax/
# Code: http://code.google.com/p/brax/

require 'webrick'

class BorgatePlugin < Plugin
  include WEBrick

  def initialize
    super

    channel = "#bor"
    @http_server = HTTPServer.new(
      :Port => 9118
    )

    @http_server.mount_proc("/say") { |req, resp|
      log "Request to speak from #{req.peeraddr[2]}"
      if req.query['nick'] && req.query['input'] && req.query['input']!='' && !req.query['input'].match(/(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?/ix)
        resp['content-type'] = 'text/plain'
        resp.body = "<#{req.query['nick']}> #{req.query['input']}"
        @bot.say channel, resp.body
        raise HTTPStatus::OK
      else
        raise HTTPStatus::PreconditionFailed.new("Missing attributes")
      end
    }

    @http_server.mount_proc("/event") { |req, resp|
      log "Event broadcast from #{req.peeraddr[2]}"
      if req.query['msg'] && req.query['msg']!=''
        resp['content-type'] = 'text/plain'
        resp.body = "** #{req.query['msg']}"
        @bot.say channel, resp.body
        raise HTTPStatus::OK
      else
        raise HTTPStatus::PreconditionFailed.new("Missing attributes")
      end
    }

    Thread.new {
      @http_server.start
    }
  end
  def cleanup
    @http_server.shutdown
    super
  end
  def help(plugin, topic="")
    "no help yet"
  end
  def privmsg(m)
  end
end

plugin = BorgatePlugin.new
plugin.register("borgate")
